package monster.com.cvh.adapter;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.text.ParseException;
import java.util.List;

import monster.com.cvh.R;
import monster.com.cvh.activity.CommentDetailsActivity;
import monster.com.cvh.activity.LoginActivity;
import monster.com.cvh.bean.NewsDetailBean;
import monster.com.cvh.bean.PostBean;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.SPUtils;
import monster.com.cvh.util.TimeConvert;
import monster.com.cvh.util.TimeUtil;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by ASUS on 2018/3/8.
 */

public class NewsDetailAdapter extends BaseQuickAdapter<NewsDetailBean.DataBean, BaseViewHolder> {

    private TextView detele;
    private String lastTime;
    private boolean is_praise;
    private TextView mTvPraise;
    private int praise;
    private int addPraise = 0;

    public NewsDetailAdapter(int layoutResId, @Nullable List<NewsDetailBean.DataBean> data) {
        super(layoutResId, data);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(final BaseViewHolder helper, final NewsDetailBean.DataBean item) {
        //获取当前时间戳
        long timeStamp = System.currentTimeMillis() / 1000;
        try {
            String s = TimeConvert.dateToStamp(item.getUpdate_time());
            Log.i("lixiaofei", "convert: "+s);
            long articleTime = timeStamp - Long.valueOf(s) / 1000;
            Log.i("lixiaofei", "convert: "+articleTime);
            lastTime = TimeUtil.calculateTime(articleTime);
            Log.i("lixiaofei", "convert: "+lastTime);
            helper.setText(R.id.tv_item_news_details_time, lastTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        helper.setText(R.id.tv_item_news_details_name, item.getUser_name());
        helper.setText(R.id.tv_item_details_text, item.getContent());
        helper.addOnClickListener(R.id.tv_item_comment_delete);
        LinearLayout ll_praise = helper.getView(R.id.ll_item_news_details);
        final ImageView mIvPraise = helper.getView(R.id.iv_item_news_details_praise);
        mTvPraise = helper.getView(R.id.tv_item_news_details_priase);
        praise = item.getPraise();
        mTvPraise.setText(String.valueOf(praise));
        is_praise = item.isIs_praise();
        Log.i(TAG, "convert: "+is_praise+helper.getLayoutPosition());
        detele = helper.getView(R.id.tv_item_comment_delete);
        Glide.with(mContext).load(item.getIcon_url()).into((ImageView) helper.getView(R.id.iv_item_news_details_avatar));
        if (String.valueOf(item.getUser_id()).equals(SPUtils.getInt(mContext, "id", -1))) {
            ll_praise.setVisibility(View.GONE);
            detele.setVisibility(View.VISIBLE);
        } else {
            detele.setVisibility(View.GONE);
            ll_praise.setVisibility(View.VISIBLE);
            if (is_praise) {
                //已经点赞
                mIvPraise.setImageResource(R.mipmap.information_praise_after);
                mIvPraise.setEnabled(false);
            } else {
                mIvPraise.setImageResource(R.mipmap.information_praise_before);
                mIvPraise.setEnabled(true);
                ll_praise.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (SPUtils.getString(mContext, "token", "") == null || SPUtils.getString(mContext, "token", "").equals("")) {
                            mContext.startActivity(new Intent(mContext, LoginActivity.class));
                        } else {
                            OkGo.<String>post(MyConstant.MAKE_PRAISE).params("token", SPUtils.getString(mContext, "token", ""))
                                    .params("is_praise", true).params("comment_id", item.getId()).execute(new StringCallback() {
                                @Override
                                public void onSuccess(String s, Call call, Response response) {
                                    Gson gosn = new Gson();
                                    PostBean bean = gosn.fromJson(s, PostBean.class);
                                    if (bean.getCode() == 1) {
                                        addPraise = item.getPraise() + 1;
                                        mIvPraise.setImageResource(R.mipmap.information_praise_after);
                                        mIvPraise.setEnabled(false);
                                        helper.setText(R.id.tv_item_news_details_priase, String.valueOf(addPraise));
                                        item.setPraise(addPraise);
                                        item.setIs_praise(true);
                                    }

                                }
                            });
                        }
                    }
                });
            }


        }
        final TextView reply = helper.getView(R.id.tv_item_news_details_reply);
        reply.setText("回复" + "(" + item.getReply() + ")");
        helper.getView(R.id.ll_details).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转评论详情
                Intent intent = new Intent(mContext, CommentDetailsActivity.class);
//                intent.putExtra("name", item.getUser_name());
//                intent.putExtra("iconUrl", item.getIcon_url());
//                intent.putExtra("content", item.getContent());
//                intent.putExtra("isPraise", is_praise);
                intent.putExtra("identify",1);
                intent.putExtra("item", item);
                intent.putExtra("time",lastTime);
                mContext.startActivity(intent);
            }
        });
    }


}
